package zhuyun.code

import javax.swing.event.TreeSelectionEvent
import javax.swing.event.TreeSelectionListener
import javax.swing.tree.DefaultMutableTreeNode
import com.sun.java.accessibility.util.SwingEventMonitor.addTreeSelectionListener
import javax.swing.*


fun main(args:Array<String>){
    val f = JFrame("外观模式:婺源特产选择测试")
    val cp = f.contentPane
    val wys = WySpecialty()
    val treeView = JScrollPane(wys.tree)
    val scrollpane = JScrollPane(wys.label)
    val splitpane = JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,treeView,scrollpane)
    splitpane.dividerLocation = 230
    splitpane.isOneTouchExpandable = true
    cp.add(splitpane)
    f.setSize(650,350)
    f.isVisible = true
    f.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
}
class WySpecialty:JPanel(),TreeSelectionListener{
    var tree: JTree? = null
    var label: JLabel? = null
    val s1 = Specialty1()
    val s2 = Specialty2()
    val s3 = Specialty3()
    val s4 = Specialty4()
    val s5 = Specialty5()
    val s6 = Specialty6()
    val s7 = Specialty7()
    val s8 = Specialty8()
    init {
        val top = DefaultMutableTreeNode("婺源特产")
        var node1: DefaultMutableTreeNode? = null
        var node2: DefaultMutableTreeNode? = null
        var tempNode: DefaultMutableTreeNode? = null
        node1 = DefaultMutableTreeNode("婺源四大特产（红、绿、黑、白）")
        tempNode = DefaultMutableTreeNode("婺源荷包红鲤鱼")
        node1.add(tempNode)
        tempNode = DefaultMutableTreeNode("婺源绿茶")
        node1.add(tempNode)
        tempNode = DefaultMutableTreeNode("婺源龙尾砚")
        node1.add(tempNode)
        tempNode = DefaultMutableTreeNode("婺源江湾雪梨")
        node1.add(tempNode)
        top.add(node1)
        node2 = DefaultMutableTreeNode("婺源其它土特产")
        tempNode = DefaultMutableTreeNode("婺源酒糟鱼")
        node2.add(tempNode)
        tempNode = DefaultMutableTreeNode("婺源糟米子糕")
        node2.add(tempNode)
        tempNode = DefaultMutableTreeNode("婺源清明果")
        node2.add(tempNode)
        tempNode = DefaultMutableTreeNode("婺源油煎灯")
        node2.add(tempNode)
        top.add(node2)
        tree = JTree(top)
        tree?.addTreeSelectionListener(this)
        label = JLabel()
    }

    override fun valueChanged(e: TreeSelectionEvent?) {
        if(e!!.source == tree){
            val node = tree?.lastSelectedPathComponent as DefaultMutableTreeNode ?: return
            if(node.isLeaf){
                val obj = node.userObject
                var sele = obj.toString()
                label!!.text = sele
                label!!.horizontalTextPosition = JLabel.CENTER
                label!!.verticalTextPosition = JLabel.BOTTOM
                sele = sele.substring(2,4)
                when(sele){
                    "荷包"->{
                        label!!.icon = s1
                    }
                    "绿茶"->{
                        label!!.icon = s2
                    }
                    "龙尾"->{
                        label!!.icon = s3
                    }
                    "江湾"->{
                        label!!.icon = s4
                    }
                    "酒糟"->{
                        label!!.icon = s5
                    }
                    "糟米"->{
                        label!!.icon = s6
                    }
                    "清明"->{
                        label!!.icon = s7
                    }
                    "油煎"->{
                        label!!.icon = s8
                    }

                }
                label!!.horizontalAlignment = JLabel.CENTER

            }

        }
    }

}

class Specialty1 : ImageIcon("src/images/卧龙谷.jpg")

class Specialty2 : ImageIcon("src/images/彩虹桥.jpg")

class Specialty3 : ImageIcon("src/images/思溪延村.jpg")
class Specialty4 : ImageIcon("src/images/晓起村.jpg")

class Specialty5 : ImageIcon("src/images/李坑.jpg")

class Specialty6 : ImageIcon("src/images/江湾.jpg")

class Specialty7 : ImageIcon("src/images/篁岭.jpg")

class Specialty8 : ImageIcon("src/images/菊径村.jpg")